/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.wbd.client  
 */
package com.apache.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;

/**
 * description:  客户端父类servlet
 * @author Hou Dayu 创建时间：2016-4-11  
 */
@SuppressWarnings("serial")
public abstract class SupperAction extends HttpServlet {

	protected Logger log = Logger.getLogger(getClass());

	protected abstract void doInvoke(HttpServletRequest request, HttpServletResponse response, String methodType)
			throws Exception;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doInvoke(request, response, "get");
		} catch (Exception e) {
			log.error("执行请求出错：", e);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doInvoke(request, response, "post");
		} catch (Exception e) {
			log.error("执行请求出错：", e);
		}
	}

	/**
	 * description:  获取请求参数
	 */
	@SuppressWarnings("rawtypes")
	protected Map<String, String> getParameterMap(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> map = new HashMap<String, String>();
		Set keSet = request.getParameterMap().entrySet();
		for (Iterator itr = keSet.iterator(); itr.hasNext();) {
			Map.Entry me = (Map.Entry) itr.next();
			String ok = String.valueOf(me.getKey());
			Object ov = me.getValue();
			String value = "";
			if (ov instanceof String[]) {
				String[] vls = (String[]) ov;
				value = vls[0];
			} else {
				value = String.valueOf(ov);
			}
			if (StrUtil.isNotNull(value)) {
				if (value.equals(new String(value.getBytes("iso8859-1"), "iso8859-1"))) {
					value = new String(value.getBytes("iso8859-1"), "utf-8");
				}
				map.put(ok, value);
			}
		}
		map.put("sysPass", ConfigUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		return map;
	}

	/**
	 * 输出json数据
	 * @param json
	 * @param response
	 * @throws IOException
	 */
	protected void outputJson(String json, HttpServletResponse response) throws IOException {
		if (StrUtil.isNotNull(json)) {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		}
	}

	/**
	 * 反射调用类方法32
	 * @param method 方法
	 * @param obj    类对象
	 * @param vo     方法数
	 * @return  返回方法执行结果
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes" })
	protected Method doInvoke(Class queryclass, HttpServletRequest request, HttpServletResponse response,
			String methodType) throws Exception {
		Method[] methods = queryclass.getDeclaredMethods();// 获得类的方法集合
		Method method = null;
		String methodName = String.valueOf(request.getAttribute("PathAttr0"));//方法名
		if (methods != null) {
			Method defMethod = null;
			int count = methods.length;
			for (int i = 0; i < count; i++) {
				if (methods[i].isAnnotationPresent(RequestMapping.class)) {
					RequestMapping rm = (RequestMapping) methods[i].getAnnotation(RequestMapping.class);
					if (rm.value().equals(methodName)) {
						if (StrUtil.isNull(rm.method())) {
							method = methods[i];
							break;
						} else if (methodType.equalsIgnoreCase(rm.method())) {
							method = methods[i];
							break;
						}
					}
					if ((StrUtil.isNull(rm.value())) && (methodType.equalsIgnoreCase(rm.method()))) {
						defMethod = methods[i];
					}
				}
			}
			if (null == method && null != defMethod) {
				method = defMethod;
			}
		}
		//		if (null == method) {//获取父类方法
		//			methods = queryclass.getSuperclass().getDeclaredMethods();
		//			if (null != methods) {
		//				for (int i = 0; i < methods.length; i++) {
		//					if (methods[i].getName().equalsIgnoreCase(methodName)) {
		//						method = queryclass.getMethod(methods[i].getName(), new Class[] { HttpServletRequest.class,
		//								HttpServletResponse.class });
		//					}
		//				}
		//			}
		//		}
		return method;
	}
}
